package com.bgs.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bgs.pojo.Employee;
import com.bgs.pojo.Mine;
import com.bgs.pojo.Money;
import com.bgs.pojo.Social;
import com.bgs.service.MineService;
import com.bgs.service.MoneyService;
import com.bgs.tool.R;
import com.bgs.utils.LocaldatetimeUtils;
import com.bgs.vo.MineVo;
import com.bgs.vo.MoneyVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 我的会议表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-05-08
 */
@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/mine")
public class MineController {

    @Autowired
    private MineService mineService;

    @ApiOperation(value = "所有我的会议表列表")
    @GetMapping("/findAll/{status}")
    public R findAllMine(@PathVariable Integer status){
        //调用service的方法实现查询所有的讲师
        List<Mine> list = mineService.list(new QueryWrapper<Mine>().eq("status",status).isNull("meetingid").notIn("meetingenter",4));
        List<MineVo> mlist = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            MineVo m = new MineVo();
            BeanUtils.copyProperties(list.get(i),m);
            m.setStarTime(LocaldatetimeUtils.timesss(list.get(i).getMeetingtime()));//时间格式转换
            m.setEndTime(LocaldatetimeUtils.timesss(list.get(i).getOvertime()));//时间格式转换
            mlist.add(m);
        }

        return R.ok().data("list",mlist);
    }

    @ApiOperation(value = "所有我的会议表列表(后台)")
    @PostMapping("/findAllht")
    public R findAllMine(@RequestBody(required = false) MineVo m){
        log.info("模糊查询前端传过来的数据MoneyVo"+m);
        PageHelper.startPage(m.getPage(),m.getLimit());
        QueryWrapper<Mine> queryWrapper = new QueryWrapper<Mine>();
        System.out.println(m.getStatus());
        if (m.getStatus() != 0) {
            queryWrapper.eq("status", m.getStatus());
        }
        if (!StringUtils.isEmpty(m.getName())) {
            queryWrapper.like("name", m.getName());
        }
        queryWrapper.isNull("meetingid");
        queryWrapper.notIn("meetingenter", 4);
        //调用service的方法实现查询所有的讲师
        List<Mine> list = mineService.list(queryWrapper);
        PageInfo<Mine> pageInfo = new PageInfo<Mine>(list);
        List<MineVo> mlist = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            MineVo ms = new MineVo();
            BeanUtils.copyProperties(list.get(i),ms);
            ms.setStarTime(LocaldatetimeUtils.timesss(list.get(i).getMeetingtime()));
            ms.setEndTime(LocaldatetimeUtils.timesss(list.get(i).getOvertime()));
            mlist.add(ms);
        }

        return  R.ok().data("total",pageInfo.getTotal()).data("rows",mlist);
    }

    @ApiOperation(value = "根据用户id查询我的会议详情+根据用户id查询我的会议详情里面的参与人")
    @GetMapping("/getIdMine/{userid}/{mid}")
    public R getIdSocial(
            @ApiParam(name = "userid", value = "用户id", required = true)
            @PathVariable Integer userid,
            @ApiParam(name = "mid", value = "会议的id", required = true)
            @PathVariable Integer mid
            ){
        List<Mine> list = mineService.list(new QueryWrapper<Mine>().eq("userid", 1).eq("mid",mid));
        MineVo m = new MineVo();
        for (int i = 0; i < list.size(); i++) {
            BeanUtils.copyProperties(list.get(i),m);
            m.setStarTime(LocaldatetimeUtils.timesss(list.get(i).getMeetingtime()));//时间格式转换
            m.setEndTime(LocaldatetimeUtils.timesss(list.get(i).getOvertime()));//时间格式转换
        }

        List<Mine> lpeo = mineService.list(new QueryWrapper<Mine>().eq("meetingid",mid));

        return R.ok().data("m",m).data("lpeo",lpeo);
    }

    @ApiOperation(value = "根据用户id查询我的会议详情里面的参与人/查询所有的参与人（根据会议id把用户把状态与用户id=0）")
    @GetMapping("/getIdMP/{mid}/{status}/{userid}")
    public R getIdMP(
            @ApiParam(name = "mid", value = "会议的id", required = true)
            @PathVariable Integer mid,
            @ApiParam(name = "status", value = "会议状态", required = true)
            @PathVariable Integer status,
            @ApiParam(name = "userid", value = "用户id", required = true)
            @PathVariable Integer userid
    ){

        List<MineVo> lpeo = mineService.listmp(mid,status,userid);

        return R.ok().data("lpeo",lpeo);
    }

    @ApiOperation(value = "添加会议")
    @PostMapping("/addMine")
    public R addMine(
            @ApiParam(name = "Mine", value = "我的会议对象", required = true)
            @RequestBody MineVo m){
        boolean save = mineService.insertSelective(m);
        if (save){
            return R.ok().data("mid",m.getMid());
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "修改会议")
    @PostMapping("/updateMine")
    public R updateMine(@RequestBody MineVo m){
        boolean flag = mineService.updateByPrimaryKeySelective(m);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation(value ="逻辑删除会议")
    @DeleteMapping("removeMine/{mid}")
    public R removeMine(
            @ApiParam(name = "id",value = "mid",required = true)
            @PathVariable Integer mid){
        boolean flag = mineService.deleteByPrimaryKey(mid);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "修改会议状态")
    @PostMapping("/updateMeetingenter")
    public R updateMeetingenter(@RequestBody Mine m){
        boolean flag = mineService.updateMeetingenter(m);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "根据会议id查询")
    @GetMapping("/getId/{mid}")
    public R getId(
            @ApiParam(name = "mid", value = "会议的id", required = true)
            @PathVariable Integer mid
    ){

        MineVo mVo = mineService.getByidtoVo(mid);

        return R.ok().data("mVo",mVo);
    }

    @ApiOperation(value = "查询所有的用户")
    @GetMapping("/getemp/{mid}")
    public R getemp( @ApiParam(name = "id",value = "mid",required = true)
                         @PathVariable Integer mid){
        List<MineVo> employee = mineService.getemp(mid);
        return R.ok().data("employee",employee);
    }

    @ApiOperation(value ="逻辑删除参与人")
    @DeleteMapping("removeemp/{mid}/{eid}")
    public R removeemp(
            @ApiParam(name = "mid",value = "会议id",required = true)
            @PathVariable Integer mid,
            @ApiParam(name = "eid",value = "参与人id",required = true)
            @PathVariable Integer eid){
        mineService.remove(new QueryWrapper<Mine>().eq("userid",eid).eq("meetingid",mid));
        return R.ok();
    }


}

